﻿using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows;


using System.Text;
using System.Windows.Forms;


namespace ProgrammersCalculator.View
{
  
    public partial class RegexView : System.Windows.Controls.UserControl
    {
        public RegexView()
        {
            InitializeComponent();
            // this.DataContext = new RegexViewModel();
        }
        private void SelectTextInRichEditor(Capture m)
        {
            richSourceText.Select(m.Index, m.Length);
            richSourceText.SelectionColor = Color.Red;
            richSourceText.SelectionBackColor = Color.Yellow;
        }

        private void SetDefaultView()
        {
            richSourceText.SelectAll();
            richSourceText.SelectionColor = Color.Black;
            richSourceText.SelectionFont = new Font("Arial", 12, System.Drawing.FontStyle.Regular);
            richSourceText.SelectionBackColor = Color.White;
        }
       
        void ReplaceSelectionWithCommandParameter(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            var textBox = (System.Windows.Controls.TextBox)sender;
            textBox.SelectedText = e.Parameter as string;
            textBox.Focus();
        }
        private void ProcessRegex()
        {
            string pattern = txtRegexExpression.Text;
            if (string.IsNullOrEmpty(pattern))
            {
                return;
            }
            SetDefaultView();

            txtSplit.Text = string.Empty;
            string searchText = richSourceText.Text;
            RegexOptions options = GetRegexOptions();

            string[] result;
            try
            {
                result = Regex.Split(searchText,
                                     pattern,
                                     options);
            }
            catch (Exception)
            {
                txtSplit.Text = "Error while parsing...";
                return;
            }

            var sb = new StringBuilder();
            foreach (string s in result)
            {
                sb.AppendLine(s);
            }
            txtSplit.Text = sb.ToString();

            Match m = Regex.Match(searchText,
                                  pattern,
                                  options);

            while (m.Success)
            {
                SelectTextInRichEditor(m);
                m = m.NextMatch();
            }
        }

        private RegexOptions GetRegexOptions()
        {
            RegexOptions options = RegexOptions.Singleline;
            if (null != isMultiline.IsChecked && isMultiline.IsChecked.Value)
            {
                options = RegexOptions.Multiline;
            }

            if (null != ignoreCaseCheckBox.IsChecked && ignoreCaseCheckBox.IsChecked.Value)
            {
                options = options | RegexOptions.IgnoreCase;
            }

            if (null != isIgnoreSpace.IsChecked && isIgnoreSpace.IsChecked.Value)
            {
                options = options | RegexOptions.IgnorePatternWhitespace;
            }
            if (null != isECMAScript.IsChecked && isECMAScript.IsChecked.Value)
            {
                options = options | RegexOptions.ECMAScript;
            }
            return options;
        }

        private void txtRegexExpression_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            ProcessRegex();

        }
    }

}
